package com.zl.csMall.mine.recAddress.controller;

import com.zl.csMall.code.LoginCacheMap;
import com.zl.csMall.code.config.token.TokenTriggerAspect;
import com.zl.csMall.mine.recAddress.po.RecAddress;
import com.zl.csMall.mine.recAddress.service.RecAddressService;
import com.zl.csMall.mine.user.po.User;
import com.zl.csMall.util.Util;
import com.zl.csMall.vo.ReturnModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @description: RecAddressController
 * @author: ChenNing
 * @date 2025-10-15 17:39
 */
@CrossOrigin(origins = "*")
@Api(tags = "用户收货地址")
@RestController
@RequestMapping("csMall/mine/addr")
public class RecAddressController {

    @Autowired
    private RecAddressService recAddressService;

    private final LoginCacheMap loginCacheMap;

    public RecAddressController(LoginCacheMap loginCacheMap) {
        this.loginCacheMap = loginCacheMap;
    }


    /**
     * @description: 查询用户收货地址
     * @author ChenNing
     * @date: 2025-10-15 17:50
     */
    @TokenTriggerAspect
    @ApiOperation(value = "查询用户收货地址", response = RecAddress.class)
    @CrossOrigin("*")
    @GetMapping
    public ReturnModel getRecAddressByUserId(@RequestHeader(required = false) String token){
        User user = loginCacheMap.getUser(token);

        List<RecAddress> recAddressList = recAddressService.getByUserId(user.getId());
        return new ReturnModel(recAddressList);
    }

    @TokenTriggerAspect
    @ApiOperation(value = "根据ID查询收货地址", response = RecAddress.class)
    @ApiImplicitParam(name = "id", value = "收货地址ID", required = true)
    @CrossOrigin("*")
    @GetMapping("{id}")
    public ReturnModel getById(@RequestHeader(required = false) String token,
                               @PathVariable("id") Long id){
        if (Util.isNullOrEmpty(id)){
            return new ReturnModel(false, "ID为空！");
        }
        RecAddress recAddress = recAddressService.getById(id);
        if (recAddress == null || recAddress.getId() == 0){
            return new ReturnModel(false, "未查询到该收货地址");
        }
        return new ReturnModel(recAddress);
    }

    /**
     * @description: 添加收货地址
     * @author ChenNing
     * @date: 2025-10-15 17:42
     */
    @CrossOrigin("*")
    @ApiOperation(value = "新增收货地址")
    @TokenTriggerAspect
    @PostMapping
    public ReturnModel addRecAddress(@RequestHeader(required = false) String token,
                                     @Validated @RequestBody RecAddress recAddress){
        User user = loginCacheMap.getUser(token);

        Date date = new Date();
        recAddress.setId(Util.getUuId());
        recAddress.setUserId(user.getId());
        if (recAddress.getIsDefault() == null){
            recAddress.setIsDefault(2);
        } else if (recAddress.getIsDefault() == 1){
            // 该地址为默认地址，取消其他的默认地址
            recAddressService.cancelDefault(user.getId());
        }
        recAddress.setCreateDate(date);
        recAddress.setUpdateDate(date);
        boolean result = recAddressService.save(recAddress);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "收货地址添加失败！");
    }

    /**
     * @description: 修改收货地址
     * @author ChenNing
     * @date: 2025-10-15 17:46
     */
    @CrossOrigin("*")
    @ApiOperation(value = "修改收货地址")
    @TokenTriggerAspect
    @PutMapping
    public ReturnModel updateRecAddress(@RequestHeader(required = false) String token,
                                        @RequestBody RecAddress recAddress){
        if (recAddress == null || recAddress.getId() == 0){
            return new ReturnModel(false, "收货地址ID不能为空！");
        }
        User user = loginCacheMap.getUser(token);

        if (recAddress.getIsDefault() != null && recAddress.getIsDefault() == 1){
            // 该地址为默认地址，取消其他的默认地址
            recAddressService.cancelDefault(user.getId());
        }
        recAddress.setUpdateDate(new Date());
        boolean result = recAddressService.updateById(recAddress);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "收货地址更新失败！");
    }

    /** 
     * @description: 删除收货地址
     * @param id 收货地址ID
     * @author ChenNing
     * @date: 2025-10-15 17:48
     */ 
    @CrossOrigin("*")
    @ApiOperation(value = "根据ID删除收货地址")
    @TokenTriggerAspect
    @DeleteMapping
    public ReturnModel deleteCustomer(@RequestHeader(required = false) String token,
                                      @RequestParam("id") Long id){
        if (Util.isNullOrEmpty(id)){
            return new ReturnModel(false, "收货地址ID为空！");
        }
        boolean result = recAddressService.removeById(id);
        if (result){
            return new ReturnModel();
        }
        return new ReturnModel(false, "收货地址删除失败！");
    }

}
